#include <iostream>
#include <cstring>
using namespace std;

const int N = 1e5 + 10;
int h[N], e[N], en[N], idx;
void add(int a, int b)
{
    e[idx] = b; en[idx] = h[a]; h[a] = idx++;
}

int q[N], d[N], hh, tt;
int n, m;

int bfs()
{
    memset(d, -1, sizeof(d));
    q[tt++] = 1;
    d[1] = 0;
    while(hh < tt)
    {
        int cur = q[hh++];
        for(int i = h[cur]; i != -1; i = en[i])
        {
            int j = e[i];
            if(d[j] != -1) continue;
            d[j] = d[cur] + 1;
            q[++tt] = j;
        }
    }
    return d[n];
}

int main()
{
    memset(h, -1, sizeof(h));
    cin >> n >> m;
    while(m--)
    {
        int a, b; cin >> a >> b;
        add(a, b);
    }
    cout << bfs();
    return 0;
}